package com.hz.framework.check;

import com.hz.framework.check.AppExtend;
import com.hz.framework.check.AppExtendImpl;
import com.hz.framework.check.Application;
import com.hz.framework.check.DesEncrypt;



public class AppExtend implements AppExtendImpl{
	
	private AppExtend(){
	}
	private static AppExtend appExtend = new AppExtend();

	public static AppExtend getInstance() {
		return appExtend;
	}
	
	private static Application ad = Application.getInstance() ;
	//将key名称存储到key的 0 扇区内 （密钥，uid）
	public String Write_name ( String name , String uid) {
		//将加密后的密钥存储到key的 1 扇区内
		String boln = ad.writeKey(uid, 0, name);
		return boln;
	}
	
	//将密钥加密后存储到key的 指定 扇区内 （密钥，uid）
	public String Write_secretKey ( String secretKey , String uid , int iBlock_index) {
		DesEncrypt des = DesEncrypt.getInstance();
		des.setKey(secretKey);//加入密钥
		String strEnc = des.getEncString(secretKey); //将密钥加密
		//将加密后的密钥存储到key的 1 扇区内
		String boln = ad.writeKey(uid, iBlock_index, strEnc);
		return boln;
	}
	
	//将密文加密后存储到key的 指定 扇区内 （密钥，信息，uid , 扇区）
	public String Write_value ( String secretKey , String value , String uid, int iBlock_index) {
		DesEncrypt des = DesEncrypt.getInstance();
		des.setKey(secretKey);//加入密钥
		String strEnc = des.getEncString(value); //将密文加密
		//将加密后的密钥存储到key的 1 扇区内
		String boln = ad.writeKey(uid, iBlock_index, strEnc);
		return boln;
	}
	
	//读取当前机器上所有key的name 0扇区的值 返回 name的数组
	public String[] read_Allname () {
		//将加密后的密钥存储到key的 1 扇区内
		String[] name = ad.readAllKey( 0 );
		return name;
	}
	//读取指定key的所有扇区内的信息 （uid）  返回信息数组
	public String read_info ( String uid , int iBlock_index ) {
		String info =  ad.readKey(uid,iBlock_index);
		info = info==null?"":info;
		return info;
	}
//	读取指定key的所有扇区内的信息 （uid）  返回信息数组
	public String[] read_Allinfo ( String uid) {
		String[] info = new String[5];
		for(int i =0;i<5;i++){
			info[i] =  ad.readKey(uid,i);
			info[i] = info[i]==null?"":info[i];
		}
		return info;
	}
	
	public static void main(String[] args) {
		
//		Application ap = new Application();
//		String uid = ap.initKey("999888");//初始化key
		String uid = Application.KEY_NUMBER; //715400947
		
//		System.out.println("init:"+uid);
//		String bo = ap.writeKey(uid , 4 , "delete");
//		System.out.println("write:"+bo);
//		String value = appExtend.read_info(uid, 0);//(uid);
//		String[] value =  ap.readAllKey( 0 );
		String keyvalue="1";
		String key = Application.KEY; //默认给密码加密的密钥
		DesEncrypt des = DesEncrypt.getInstance();
		des.setKey(key);//加入密钥
		String value = appExtend.read_info(uid, 0);//(uid); 读取0扇区的密码
		System.out.println("0read:"+des.getDesString(value));
//		appExtend.Write_value(key, keyvalue, uid, 0);
//		appExtend.Write_value(keyvalue, "zj9", uid, 1);
//		appExtend.Write_value(keyvalue, "住建厅", uid, 2);
//		appExtend.Write_value(keyvalue, "无", uid, 3);
//		appExtend.Write_value(keyvalue, "无", uid, 4);
		
		des.setKey(des.getDesString(value));
		for(int i =1;i<5;i++){
			value = appExtend.read_info(uid, i);//(uid);
			System.out.println(i+"read:"+value);
			System.out.println(i+"read:"+des.getDesString(value));
		}
		//System.out.println("read:"+value);
//		read:PhHmK1fOujs=
//		read:QsUq0b+l3W4=
//		read:41w8gqNw9is=
//		read:m5plOOLfM/8=
	}

}